PostgreSQL upgrade
1 本章说明
使用 git clone 命令下载源代码并完成 PostgreSQL 最新版安装后,接下来如何在此基础上升级最新的 PostgreSQL 数据库?本章将会介绍升级的具体步骤。
2 准备工作
2.1 下载最新的源码
#postgres>
cd /soft
rm -rf postgresql
git clone https://git.postgresql.org/git/postgresql.git
cd postgresql
git tag # 选择最新的标签
git checkout REL_18_RC1
Note: switching to 'REL_18_RC1'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 62006223013 Stamp 18rc1.
2.2 关闭数据库
pg_ctl stop -D $PGDATA
2.3 保留老版本的软件与数据
Note
由于数据目录目前存放在 /usr/local/pgsql 目录下,所以 mv 命令也能够备份
数据库文件。
#root>
mv /usr/local/pgsql /usr/local/pgsql.old
mkdir -p /usr/local/pgsql
chown postgres:postgres /usr/local/pgsql
3 编译postgresql 软件包并安装到指定目录
3.1 编译配置
#postgres>
cd /soft/postgresql/
./configure --prefix=/usr/local/pgsql --with-llvm --with-systemd --with-ssl=openssl --enable-debug --with-icu --enable-nls --with-zlib --with-libxml --with-perl --with-ldap --with-python --with-libxslt --with-uuid=e2fs --with-pam --with-gssapi --with-zstd --with-lz4 --with-tcl --enable-depend --enable-cassert --enable-debug --enable-dtrace CFLAGS="-ggdb -O0"
#postgres>
make -j 8 && make install
3.2 构建文档
#postgres>
make install-docs
3.3 编译第三方插件并安装
#postgres>
#包括第三方插件全部编译
cd /soft/postgresql*/contrib
#这个需要使用普通用户执行,可选,耗时较长
make -j 8 && make install
4 创建数据库集簇
4.1 创建目录
#postgres>
mkdir -p /usr/local/pgsql/data
4.2 初始化数据库集簇
#postgres>
initdb -D $PGDATA -W --data-checksums -A scram-sha-256 -E UTF-8
Warning
- 要想初始化集簇为英文,需要设置
LANG=en_US.UTF-8。 - 更多关于Postgersql 能够初始化的字符集请见 PostgreSQL: Documentation: 15: 24.3. Character Set Support
- 复制时需要 data-cecksums 支持块校验。
postgres用户的密码为postgres
4.3 调整 pg_hba.conf 文件
vi $PGDATA/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
4.4 自定义全文搜索文件
将任何自定义全文搜索文件(词典、同义词、同义词库、停用词)从旧集群复制到新集群。
5 升级数据库
5.1 运行升级命令
su - postgres
cd ~
pg_upgrade \
--old-datadir "/usr/local/pgsql.old/data" \
--new-datadir "/usr/local/pgsql/data" \
--old-bindir "/usr/local/pgsql.old//bin" \
--new-bindir "/usr/local/pgsql/bin"
5.2 配置文件恢复
cp /usr/local/pgsql.old/data/postgresql.conf $PGDATA/
cp /usr/local/pgsql.old/data/pg_hba.conf $PGDATA/
cp /usr/local/pgsql.old/data/postgresql.auto.conf $PGDATA/
cp /usr/local/pgsql.old/data/err_log.conf $PGDATA/
5.3 启动数据库
pg_ctl start -D $PGDATA
5.4 收集统计信息并删除老版本
Some statistics are not transferred by pg_upgrade.
Once you start the new server, consider running these two commands:
/usr/local/pgsql/bin/vacuumdb --all --analyze-in-stages --missing-stats-only
/usr/local/pgsql/bin/vacuumdb --all --analyze-only
5.5 运行升级脚本
Warning
这个脚本会启用扩展。
如果没有使用扩展则不会生成脚本,此步骤可以忽略。
如果需要任何升级后处理,pg_upgrade 会在升级完成时会生成脚本文件,管理员必须运行。脚本文件会连接到每个需要升级后处理的数据库。每个脚本应使用以下方式运行:
psql --username=postgres --file=script.sql postgres
5.6 删除老版本
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
6 回退老版本
rm -rf /usr/local/pgsql
mv /usr/local/pgsql.old/ /usr/local/pgsql
su - postgres
pg_ctl start -D /data
7 参考文档
PostgreSQL:文档:18:pg_upgrade --- PostgreSQL: Documentation: 18: pg_upgrade